package com.cuckoo.mall.thus.start;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.EdgeEffectCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bgn.baseframe.base.BaseActivity;
import com.bgn.baseframe.utils.PreferUtil;
import com.bgn.baseframe.utils.UiUtil;
import com.bgn.baseframe.view.indicator.ScaleCircleNavigator;
import com.cuckoo.mall.R;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：引导页的Activity
 * 创建人：wl
 * 创建时间：2016/4/19 9:43
 */
public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {


    private List<ImageView> imageViewList;
    private TextView btnStartExperience;

    ViewPager mViewPager;

    private EdgeEffectCompat rightEdge;

    private RelativeLayout rly_root;
    MagicIndicator magicIndicator;

    //引导页数量以及显示的图片资源id
    int[] imageIDs = {R.mipmap.guide_page01, R.mipmap.guide_page02, R.mipmap.guide_page03, R.mipmap.guide_page04};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setIsLoadTitleBar(false);
        // 设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guide);
        getStateBar().hide();
        initView();
    }


    /**
     * 初始化控件
     */
    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.vp_guide);
        btnStartExperience = (TextView) findViewById(R.id.btn_guide_start_experience);
        rly_root = findViewById(R.id.rly_root);
        magicIndicator = findViewById(R.id.magic_indicator);
        imageViewList = new ArrayList<>();
        ImageView iv;
        for (int i = 0; i < imageIDs.length; i++) {
            iv = new ImageView(this);
            iv.setScaleType(ImageView.ScaleType.CENTER);
            iv.setBackgroundResource(imageIDs[i]);
            imageViewList.add(iv);
            iv.setOnClickListener(this);

        }

        // 把Adapter适配器和ViewPager关联起来
        GuideAdapter mAdapter = new GuideAdapter();
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(this);
        btnStartExperience.setOnClickListener(this);
        rly_root.setOnClickListener(this);
        initEdgeEffectCompat(mViewPager);
        initIndicator();
    }


    private void initEdgeEffectCompat(ViewPager viewPager) {
        try {
            Field rightEdgeField = viewPager.getClass().getDeclaredField("mRightEdge");
            if (rightEdgeField != null) {
                rightEdgeField.setAccessible(true);
                rightEdge = (EdgeEffectCompat) rightEdgeField.get(viewPager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // 把对应position位置的Imageview在ViewPager中移除掉
            container.removeView(imageViewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // 向ViewPager中添加一个ImageView对象, 并且在此方法中把那个被添加的imageView对象返回
            ImageView imageView = imageViewList.get(position);
            container.addView(imageView);
            return imageView;
        }
    }


    /**
     * 当滚动时
     * position 目前被选中的页面的索引
     * positionOffset 移动到下一个页面的比例, 范围是: 0.0 ~ 1.0
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * 当新的页面被选中时
     */
    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
            case 1:
            case 2:
                btnStartExperience.setVisibility(View.GONE);
                break;
            default:
                btnStartExperience.setVisibility(View.VISIBLE);
                break;
        }

    }

    /**
     * 当页面滚动的状态改变时
     */
    @Override
    public void onPageScrollStateChanged(int state) {
        //到了最后一张并且还继续拖动，出现蓝色限制边条了
        if (rightEdge != null && !rightEdge.isFinished()) {
            onClick(btnStartExperience);
        }
    }

    @Override
    public void onClick(View v) {

        if (mViewPager.getCurrentItem() != imageIDs.length - 1) {
            mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
        } else {
            // 把是否打开主页面的值标志为true, 后面就不会再打开引导页面
            PreferUtil.putBoolean(PreferUtil.KEY_FIRST_START, false);
            //跳转到首页
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    private void initIndicator() {
        magicIndicator.setBackgroundColor(Color.WHITE);
        // BezierPagerIndicator bezierPagerIndicator = new BezierPagerIndicator(this);

        ScaleCircleNavigator scaleCircleNavigator = new ScaleCircleNavigator(this);
        scaleCircleNavigator.setCircleCount(4);
        scaleCircleNavigator.setCircleSpacing(80);
        scaleCircleNavigator.setNormalCircleColor(UiUtil.getColor(R.color.gray_dddddd));
        scaleCircleNavigator.setSelectedCircleColor(Color.RED);
        scaleCircleNavigator.setCircleClickListener(new ScaleCircleNavigator.OnCircleClickListener() {
            @Override
            public void onClick(int index) {
                mViewPager.setCurrentItem(index);
            }
        });
        magicIndicator.setNavigator(scaleCircleNavigator);
        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }
}
